package com.xilihui.xlh.business.adapters;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.ApplyActivity;
import com.xilihui.xlh.business.activitys.GoAppointmenActivity;
import com.xilihui.xlh.business.activitys.MergePaymentsActivity;
import com.xilihui.xlh.business.activitys.MyChecklistActivity;
import com.xilihui.xlh.business.activitys.StartAppointmentActivity;
import com.xilihui.xlh.business.activitys.TakeGoodsActivity;
import com.xilihui.xlh.business.dialogs.GeneralDialogFragment;
import com.xilihui.xlh.business.dialogs.MaterialDialogFragment;
import com.xilihui.xlh.business.entities.AppointmentEntity;
import com.xilihui.xlh.business.interfaces.OnNegativeClickListener;
import com.xilihui.xlh.business.interfaces.OnPositiveClickListener;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

import rx.Observable;

/**
 * Created by Administrator on 2018/6/26.
 */

public class MyAppointmentAdapter extends RecyclerView.Adapter<BaseViewHolder>{
    
    private static final int normal = 1;
    private static final int hint = 2;
    
    public static final String SELECT = "select";
    public static final String CANCEL = "cancel";
    
    int status = 0;
    FragmentActivity activity;
    
    ArrayList<AppointmentEntity.DataBean.AppointBean> datas = new ArrayList<>();
    ArrayList<AppointmentEntity.DataBean.AppointBean> selects = new ArrayList<>();
    
    public MyAppointmentAdapter(FragmentActivity fragmentActivity,int status){
        this.activity = fragmentActivity;
        this.status = status;
    }
    
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType){
            case normal:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_me_appointment,
                        parent,false);
                break;
            case hint:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_wait_pay,
                        parent,false);
                break;
        }
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {

        switch (getItemViewType(position)){
            case normal:
                showData(holder,position);
                if(status==2){
                    holder.setVisibility(R.id.checkbox,View.VISIBLE);
                }else {
                    holder.setVisibility(R.id.checkbox,View.GONE);
                }
                break;
            case hint:
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(activity, MergePaymentsActivity.class);
                        intent.putExtra("wa_ids","");
                        intent.putExtra("id",datas.get(position).getSa_id());
                        activity.startActivity(intent);
                    }
                });
                break;
        }
        
    }

    @Override
    public int getItemCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(datas.get(position).getType()==2){
            return hint;
        }else {
            return normal;
        }
    }
    
    public void setData(ArrayList<AppointmentEntity.DataBean.AppointBean> data){
        if(datas.size()>0){
            datas.clear();
        }
        
        datas.addAll(data);
        notifyDataSetChanged();
    }
    
    public void showData(final BaseViewHolder holder, final int position){
        
        if(getItemViewType(position)==1){
            holder.setText(R.id.tv_shopName,datas.get(position).getAgent().getName());

            switch (Integer.valueOf(datas.get(position).getStatus())){
                case 0:
                    holder.setText(R.id.tv_waitStart,"等待确认开始");
                    break;
                case 1:
                    holder.setText(R.id.tv_waitStart,"等待确认结束");
                    break;
                case 2:
                    holder.setText(R.id.tv_waitStart,"等待确认付款");
                    break;
                case 3:
                    holder.setText(R.id.tv_waitStart,"已结账完成 ");
                    break;
                case 4:
                    holder.setText(R.id.tv_waitStart,"已结账完成");
                    break;
                case 5:
                    holder.setText(R.id.tv_waitStart,"店取消");
                    break;
            }
            
            switch (status){
                case 0:
                    holder.setVisibility(R.id.tv_ddqx,View.VISIBLE);
                    holder.setVisibility(R.id.tv_cancle,View.VISIBLE);
                    holder.setVisibility(R.id.tv_take,View.VISIBLE);
                    holder.setVisibility(R.id.tv_startAppointment,View.VISIBLE);
                    holder.setText(R.id.tv_startAppointment,"开始工作");
                    break;
                case 1:
                    holder.setVisibility(R.id.tv_ddqx,View.GONE);
                    holder.setVisibility(R.id.tv_cancle,View.GONE);
                    holder.setVisibility(R.id.tv_take,View.VISIBLE);
                    holder.setVisibility(R.id.tv_startAppointment,View.VISIBLE);
                    holder.setText(R.id.tv_startAppointment,"结束工作");
                    break;
                case 2:
                    holder.setVisibility(R.id.tv_ddqx,View.GONE);
                    holder.setVisibility(R.id.tv_cancle,View.GONE);
                    holder.setVisibility(R.id.tv_take,View.GONE);
                    holder.setVisibility(R.id.tv_startAppointment,View.GONE);
                    
                    holder.setVisibility(R.id.checkbox,View.VISIBLE);
                    final CheckBox checkBox = (CheckBox) holder.getView(R.id.checkbox);
                    if(datas.get(position).isSelect()){
                        checkBox.setChecked(true);
                    }else {
                        checkBox.setChecked(false);
                    }
                    
//                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                        @Override
//                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                            LogUtil.i("mylog","b:"+b);
//                            if(b){
//                                selects.add(datas.get(position));
//                            }else {
//                                selects.remove(datas.get(position));
//                            }
//                        }
//                    });
                    
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            
                            if(datas.get(position).isSelect()){
                                checkBox.setChecked(false);
                                datas.get(position).setSelect(false);
                            }else {
                                checkBox.setChecked(true);
                                datas.get(position).setSelect(true);
                            }
                            YEventBuses.post(new YEventBuses.Event(SELECT));
                            notifyDataSetChanged();
                        }
                    });
                    
                    
                    break;
            }

            holder.setText(R.id.iv_address,datas.get(position).getAgent().getAddress());
            holder.setText(R.id.tv_time,datas.get(position).getStart_time()+"~"+datas.get(position).getEnd_time());
            String type = datas.get(position).getHair_project().getName();
            if(!TextUtil.isEmpty(type)){
                holder.setText(R.id.tv_type,type);
            }
            holder.setText(R.id.tv_seat,datas.get(position).getSeat().getName());
            
            String remark = datas.get(position).getRemark();
            if(TextUtils.isEmpty(remark)){
                holder.setVisibility(R.id.tv_beizhu,View.GONE);
            }else {
                holder.setVisibility(R.id.tv_beizhu,View.VISIBLE);
                holder.setText(R.id.tv_beizhu,remark);
            }
            
            holder.setOnClickListener(R.id.iv_phone, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + datas.get(position).getAgent().getPhone());
                    intent.setData(data);
                    activity.startActivity(intent);
                }
            });
            
            holder.setOnClickListener(R.id.tv_ddqx, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    daoDianCancle(view,datas.get(position).getId(),position);
                }
            });
            
            holder.setOnClickListener(R.id.tv_cancle, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cancle(view,datas.get(position).getId());
                }
            });
            
            holder.setOnClickListener(R.id.tv_startAppointment, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(),StartAppointmentActivity.class);
                    intent.putExtra("data",datas.get(position));
                    intent.putExtra("status",status);
                    view.getContext().startActivity(intent);
                }
            });
            
            holder.setOnClickListener(R.id.tv_take, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(),TakeGoodsActivity.class);
                    intent.putExtra("id",datas.get(position).getAgent().getId());
                    intent.putExtra("wa_id",datas.get(position).getId());
                    view.getContext().startActivity(intent);
                }
            });
            
        }
        
    }
    
    //取消预约
    public void cancle(final View view, final String id){


        AppointmentRequest.hint(activity,id)
                .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                        builder.setMessage(entity.getMsg());
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                MyAppointmentRequest.cancelAppoint(view.getContext(),id)
                                        .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                                        .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                            @Override
                                            protected void onSuccess(final BaseEntity entity) {
                                                ToastUtil.toastShortPositive(entity.getMsg());
                                                LogUtil.i("mylog","quxiao");
                                                YEventBuses.post(new YEventBuses.Event(MyAppointmentAdapter.CANCEL));
                                            }

                                            @Override
                                            protected void onFail(String msg) {
                                                ToastUtil.toastShortNegative(msg);
                                            }
                                        });
                            }
                        });

                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.show();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
       
        
        
    }
    
    //到店取消
    public void daoDianCancle(final  View view, final  String id, final int position){
        
        MyAppointmentRequest.paying(activity,id)
                .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                .subscribe(new HttpSubscriber<BaseEntity>(activity) {

                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        if(TextUtil.isEmpty(entity.getMsg())){
                            Intent intent = new Intent(view.getContext(),StartAppointmentActivity.class);
                            intent.putExtra("data",datas.get(position));
                            intent.putExtra("status",2);
                            view.getContext().startActivity(intent);
                        }else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setMessage(entity.getMsg());
                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(view.getContext(),StartAppointmentActivity.class);
                                    intent.putExtra("data",datas.get(position));
                                    intent.putExtra("status",2);
                                    view.getContext().startActivity(intent);
                                }
                            });
                            
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    
                                }
                            });

                            builder.show();
                        }
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }
    
    public ArrayList<AppointmentEntity.DataBean.AppointBean> getSelect(){
        
        selects = new ArrayList<>();
        for (int i=0;i<datas.size();i++){
            if(datas.get(i).isSelect()){
                selects.add(datas.get(i));
            }
        }
        return selects;
    }
    
    public ArrayList<AppointmentEntity.DataBean.AppointBean> getSelects(){
        return selects;
    }

    /**
     * 全选 或 全取消
     * @param flag
     */
    public void isAllSelectOrCancel(boolean flag){
        selects.clear();
        for (int i=0;i<datas.size();i++){
            datas.get(i).setSelect(flag);
            if(flag){
                selects.add(datas.get(i));
            }
        }
        notifyDataSetChanged();
    }
    
}
